<template>
  <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <h2 class="sub-header">编辑</h2>
    <form>
      <div class="form-group">
        <label for>姓名</label>
        <input
          type="text"
          class="form-control"
          id
          name="name"
          required
          minlength="2"
          maxlength="10"
          v-model="student.name"
        />
      </div>
      <div class="form-group">
        <label for>性别</label>
        <div>
          <label class="radio-inline">
            <input type="radio" name="gender" id value="0" v-model="student.gender" /> 男
          </label>
          <label class="radio-inline">
            <input type="radio" name="gender" id value="1" v-model="student.gender" /> 女
          </label>
        </div>
      </div>
      <div class="form-group">
        <label for>年龄</label>
        <input
          class="form-control"
          type="number"
          id
          name="age"
          v-model="student.age"
          required
          min="1"
          max="150"
        />
      </div>
      <div class="form-group">
        <label for>爱好</label>
        <input class="form-control" type="text" id name="hobbies" value v-model="student.hobbies" />
      </div>
      <button class="btn btn-success" type="button" @click="doSubmit">提交</button>
    </form>
  </div>
</template>
<script>
import { Toast } from "mint-ui";
import { editStudentData, editStudentPostData } from "@/apis/data";
export default {
  data() {
    return {
      student: {}
    };
  },
  methods: {
    getStudent() {
      var id = this.$route.params.id;
      editStudentData(id).then(response => {
        this.student = response.data[0];
      });
    },
    doSubmit() {
      editStudentPostData(this.student).then(response => {
        var code = response.data;
        if (code.err_code == 0) {
          Toast({
            message: "修改信息成功！",
            position: "middle",
            duration: 1000,
            iconClass: "glyphicon glyphicon-ok"
          });
          this.$router.push("/students")
        } else if (code.err_code == 1) {
          Toast({
            message: "姓名不能为空！",
            position: "middle",
            duration: 1000,
            iconClass: "glyphicon glyphicon-remove"
          });
        } else if (code.err_code == 2) {
          Toast({
            message: "爱好不能为空！",
            position: "middle",
            duration: 1000,
            iconClass: "glyphicon glyphicon-remove"
          });
        } else if (code.err_code == 3) {
          Toast({
            message: "年龄不能为空！",
            position: "middle",
            duration: 1000,
            iconClass: "glyphicon glyphicon-remove"
          });
        } else if (code.err_code == 4) {
          Toast({
            message: "性别错误！",
            position: "middle",
            duration: 1000,
            iconClass: "glyphicon glyphicon-remove"
          });
        } else if (code.err_code == 5) {
          Toast({
            message: "姓名长度必须为2到10位",
            position: "middle",
            duration: 1000,
            iconClass: "glyphicon glyphicon-remove"
          });
        } else if (code.err_code == 6) {
          Toast({
            message: "年龄必须为数字",
            position: "middle",
            duration: 1000,
            iconClass: "glyphicon glyphicon-remove"
          });
        } else if (code.err_code == 7) {
          Toast({
            message: "年龄必须为0到150岁之间",
            position: "middle",
            duration: 1000,
            iconClass: "glyphicon glyphicon-remove"
          });
        } else if (code.err_code == 8) {
          Toast({
            message: "爱好不能少于四位",
            position: "middle",
            duration: 1000,
            iconClass: "glyphicon glyphicon-remove"
          });
        }
      });
    }
  },
  created() {
    this.getStudent();
  }
};
</script>
<style>
</style>
